package com.chenglai.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.order.dao.MainOrderMapper;
import com.chenglai.order.dao.OrderMapper;
import com.chenglai.order.pojo.MainOrder;
import com.chenglai.order.pojo.Order;
import com.chenglai.order.rabbitmq.MqServer;
import com.chenglai.order.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.applet.Main;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiang
 * @created 2023/11/17 11:30:23
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private MainOrderMapper mainOrderMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MqServer mqServer;

    private Map map;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Map toAddMainOrders(MainOrder mainOrder) {
        map = new HashMap();
        //订单号
        String orderNum = String.valueOf(new Date().getTime());
        mainOrder.setOrderNum(orderNum);
        mainOrder.setOrderStatus(0);
        mainOrder.setOrderTime(DateFormatHelper.currentTimes());
//        mainOrderMapper.addMainOrder(mainOrder);
//        for (Order order : orders) {
//            order.setOrderNum(orderNum);
//            order.setCommentStatus(0);
//            order.setShopId(mainOrder.getShopId());
//        }
//        orderMapper.insertList(orders);
//        map.put("mainOrder", mainOrder);

        map.put("orderNum",orderNum);
        map.put("userId",mainOrder.getUserId());



        redisTemplate.opsForHash().put("orders_"+mainOrder.getUserId().toString(),"m_"+mainOrder.getOrderNum(), JSON.toJSONString(mainOrder));
        redisTemplate.opsForHash().put("orders_"+mainOrder.getUserId().toString(),"e_"+mainOrder.getOrderNum(),JSON.toJSONString(mainOrder.getOrderList()));

        //添加到队列
        mqServer.sendOrderMessage(map);
        map = new HashMap();
        map.put("orderNum",orderNum);
        return map;

    }

    @Override
    public void addMainOrderWuliu(Integer moId, String orderWuliu, String orderCom, String orderWuliuName, Double postSum) {
        MainOrder mainOrder = new MainOrder();
        mainOrder.setOrderWuliu(orderWuliu);
        mainOrder.setOrderWuliuName(orderWuliuName);
        mainOrder.setOrderCom(orderCom);
        mainOrder.setPostSum(postSum);
        mainOrder.setMoId(moId);
        mainOrderMapper.updateByPrimaryKey(mainOrder);
    }

    @Override
    public List<MainOrder> getMainOrder(Integer userId, Integer shopId, Integer status) {
        Example example = new Example(MainOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if (status == 0)
            criteria.andEqualTo("userId", userId).andEqualTo("shopId", shopId);
        else
            criteria.andEqualTo("userId", userId).andEqualTo("shopId", shopId).andEqualTo("orderStatus", status);

        List<MainOrder> mainOrders = mainOrderMapper.selectByExample(example);
        for (MainOrder mainOrder : mainOrders) {
            example = new Example(Order.class);
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andEqualTo("orderNum", mainOrder.getOrderNum());
            List<Order> orders = orderMapper.selectByExample(example);
            mainOrder.setOrderList(orders);
        }
        return mainOrders;
    }

    @Override
    public PageInfo getMainOrderList(Integer page, Integer size, Integer shopId, Integer status) {
        PageHelper.startPage(page, size);
        Example example = new Example(MainOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if (status == 0)
            criteria.andEqualTo("shopId", shopId);
        else
            criteria.andEqualTo("shopId", shopId).andEqualTo("orderStatus", status);
        example.orderBy("moId").desc();
        List<MainOrder> mainOrders = mainOrderMapper.selectByExample(example);
        return new PageInfo(mainOrders);
    }

    @Override
    public void updateOrderStatus(String orderNum, Integer status, String transactionId) {
        Example example = new Example(MainOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderNum", orderNum);
        MainOrder mainOrder = new MainOrder();
        mainOrder.setOrderStatus(status);
        mainOrder.setTransactionId(transactionId);
        mainOrderMapper.updateByExampleSelective(mainOrder, example);
    }

    @Override
    public void updateOrderRefundStatus(String transactionId, Integer status) {
        Example example = new Example(MainOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("transactionId", transactionId);
        MainOrder mainOrder = new MainOrder();
        mainOrder.setOrderStatus(status);
        mainOrderMapper.updateByExampleSelective(mainOrder, example);
    }

    @Override
    public void addMainOrder(MainOrder mainOrder) {
        mainOrderMapper.insertSelective(mainOrder);
        orderMapper.insertList(mainOrder.getOrderList());
    }

    public Integer queryOrderFrom(String orderNum) {
        return mainOrderMapper.queryOrderFrom(orderNum);
    }

    @Override
    public Map<String, Object> detailByGroupOn(String orderNum) {
        Map<String, Object> map = mainOrderMapper.detailByGroupOn(orderNum);
        Integer goodsId = mainOrderMapper.queryGoodsId(orderNum);
        map.put("goodsId", goodsId);
        map.put("orderNum", orderNum);
        return map;
    }

    @Override
    public Map<String, Object> queryRefundParam(String orderNum) {
        return mainOrderMapper.queryRefundParam(orderNum);
    }

    @Override
    public void updateOrderGroup(String orderNum, Integer groupId) {
        Example example = new Example(MainOrder.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andEqualTo("orderNum",orderNum);
        MainOrder mainOrder = new MainOrder();
        mainOrder.setGroupId(groupId);
        mainOrderMapper.updateByExampleSelective(mainOrder,example);
    }

    @Override
    public void updateOrderGroupStatus(Integer groupId, Integer status) {
        Example example = new Example(MainOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("gtoupId",groupId);
        MainOrder mainOrder = new MainOrder();
        mainOrder.setIsGroupSuccess(1);
        mainOrderMapper.updateByExampleSelective(mainOrder,example);

    }
}
